<template>
  <div class="xtx-cart-page">
    <div class="container">
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem>购物车</XtxBreadItem>
      </XtxBread>
      <div class="cart">
        <table>
          <thead>
            <tr>
              <th width="120">
                <!-- 直接从vuex中获取不符合vuex中的逻辑 allSelected是从vuex中的getter中获取到的 -->
                <!-- 点击复选框 -->
                <XtxCheckbox :model-value="allSelected" @change="allCheck"
                  >全选</XtxCheckbox
                >
              </th>
              <th width="400">商品信息</th>
              <th width="220">单价</th>
              <th width="180">数量</th>
              <th width="180">小计</th>
              <th width="140">操作</th>
            </tr>
          </thead>
          <!-- 有效商品 -->
          <tbody>
            <tr v-for="i in cartList" :key="i">
              <td>
                <XtxCheckbox
                  :model-value="i.selected"
                  @change="(selected) => checkChange(i, selected)"
                />
              </td>
              <td>
                <div class="goods">
                  <RouterLink to="/"
                    ><img :src="i.picture" alt=""
                  /></RouterLink>
                  <div>
                    <p class="name ellipsis">
                      {{ i.name }}
                    </p>
                    <!-- 选择规格组件 -->
                  </div>
                </div>
              </td>
              <td class="tc">
                <p>&yen;{{ i.nowOriginalPrice }}</p>
                <p>
                  比加入时降价
                  <span class="red">&yen;{{ i.price - i.nowPrice }}</span>
                </p>
              </td>
              <td class="tc">
                <XtxNumbox
                  :isLabel="quantity"
                  :model-value="i.count"
                  @change="(num) => numChange(i, num)"
                />
              </td>
              <td class="tc">
                <p class="f16 red">
                  &yen;{{ (i.nowPrice * i.count).toFixed(2) }}
                </p>
              </td>
              <td class="tc">
                <p><a class="green" href="javascript:;" @click="delMerchandise(i.skuId)">删除</a></p>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <!-- 操作栏 -->
      <div class="action">
        <div class="batch">
          共 {{ allCount }} 件商品，已选择 {{ selectedCount }} 件，商品合计：
          <span class="red">¥{{ selectedPrice.toFixed(2) }}</span>
        </div>
        <div class="total">
          <XtxButton type="primary" @click="settlement">下单结算</XtxButton>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { computed, ref } from 'vue'
import { useStore } from 'vuex'

import { MessageTest } from '@/components/Message/index.js'
import { useRouter } from 'vue-router'
export default {
  name: 'XtxCartPage',
  setup () {
    const store = useStore()
    const router = useRouter()
    // 点击单选改变状态
    function checkChange (curItem, selected) {
      // console.log(curItem, selected)
      store.dispatch('cart/asyncCheck', {
        skuId: curItem.skuId,
        count: curItem.count,
        selected
      })
    }

    function allCheck (seleted) {
      // 得到点击全选之后的状态
      // console.log(seleted)
      // 点击全选 去影响 小选 把状态传递给vuex中的allCheck方法
      store.dispatch('cart/allCheck', seleted)
    }
    // 数量展示
    const quantity = ref(false)
    // 购物车页面数量展示
    function numChange (curItem, count) {
      // console.log(curItem, selected);
      store.dispatch('cart/asyncNumChange', {
        skuId: curItem.skuId,
        count,
        selected: curItem.selected
      })
    }
    // 计算得到购物车选中的数量
    const selectedCount = computed(() => store.getters['cart/selectedCount'])
    // 下单结算
    function settlement () {
      // 判断选中的数量是否大于0
      if (selectedCount.value > 0) {
        // 跳转路由
        router.push('/settlement')
      } else {
        // 提示没有选购的商品
        MessageTest({ text: '没有选购的商品', type: 'warn' })
      }
    }
    // 购物车删除
    function delMerchandise (id) {
      // console.log(id);
      store.dispatch('cart/asyncDelCart', [id])
    }
    return {
      cartList: computed(() => store.state.cart.list),
      allCount: computed(() => store.getters['cart/allCount']),
      allSelected: computed(() => store.getters['cart/allSelected']),
      selectedCount,
      selectedPrice: computed(() => store.getters['cart/selectedPrice']),
      checkChange,
      allCheck,
      quantity,
      numChange,
      settlement,
      delMerchandise
    }
  }
}
</script>
<style scoped lang="less">
.tc {
  text-align: center;
  .xtx-numbox {
    margin: 0 auto;
    width: 5rem;
  }
}
.red {
  color: @priceColor;
}
.green {
  color: @xtxColor;
}
.f16 {
  font-size: 0.667rem;
}
.goods {
  display: flex;
  align-items: center;
  img {
    width: 4.167rem;
    height: 4.167rem;
  }
  > div {
    width: 11.667rem;
    font-size: 0.667rem;
    padding-left: 0.417rem;
    .attr {
      font-size: 0.583rem;
      color: #999;
    }
  }
}
.action {
  display: flex;
  background: #fff;
  margin-top: 0.833rem;
  height: 3.333rem;
  align-items: center;
  font-size: 0.667rem;
  justify-content: space-between;
  padding: 0 1.25rem;
  .xtx-checkbox {
    color: #999;
  }
  .batch {
    a {
      margin-left: 0.833rem;
    }
  }
  .red {
    font-size: 0.75rem;
    margin-right: 0.833rem;
    font-weight: bold;
  }
}
.tit {
  color: #666;
  font-size: 0.667rem;
  font-weight: normal;
  line-height: 2.083rem;
}
.xtx-cart-page {
  .cart {
    background: #fff;
    color: #666;
    table {
      border-spacing: 0;
      border-collapse: collapse;
      line-height: 1rem;
      th,
      td {
        padding: 0.417rem;
        border-bottom: 0.042rem solid #f5f5f5;
        &:first-child {
          text-align: left;
          padding-left: 1.25rem;
          color: #999;
        }
      }
      th {
        font-size: 0.667rem;
        font-weight: normal;
        line-height: 2.083rem;
      }
    }
  }
}
</style>
